package cn.bs.busuo.account.service.impl;

import cn.bs.busuo.account.dao.cache.IFriendCacheRepository;
import cn.bs.busuo.account.dao.persist.repository.IFriendRepository;
import cn.bs.busuo.account.pojo.vo.FriendsVO;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.account.dao.persist.repository.IUserRepository;
import cn.bs.busuo.account.pojo.entity.Friends;
import cn.bs.busuo.account.service.IFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements IFriendService {
    @Autowired
    private IFriendRepository friendRepository;
    @Autowired
    private IFriendCacheRepository friendCacheRepository;

    @Override
    public int addNewFriend(Long userId,Long id) {
        if (userId == id) {
            String message = "添加好友失败，不能添加自己为好友！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        if (friendRepository.selectFriendIfExits(userId,id) > 0) {
            String message = "添加好友失败，已经添加过此好友！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Friends friend = new Friends();
        friend.setUserId(userId);
        friend.setFriendId(id);
        friendCacheRepository.addNewFriend(userId, id);
        return friendRepository.addNewFriend(friend);
    }

    @Override
    public int deleteFriend(Long userId, Long id) {
        if (userId == id) {
            String message = "删除好友失败，不能删除自己！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        if (friendRepository.selectFriendIfExits(userId,id) == 0) {
            String message = "删除好友失败，此好友不存在！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        friendCacheRepository.deleteFriend(userId, id);
        return friendRepository.deleteFriend(userId,id);
    }

    @Override
    public List<FriendsVO> listFriend(Long userId) {
        List<FriendsVO> friendsVOS = friendRepository.listFriend(userId);
        Boolean IfAddCacheSuccess = friendCacheRepository.addFriendList(userId, friendsVOS);

        return null;
    }
}
